package sz.szsmk.citizencard.ui.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.entity.UploadHeadImgEntry;


/**
 * Created by hyt on 2019/4/17.
 */
public class UpLoadHeadImgDialog extends Dialog {


    ImageView iv_close;

    Button btn_ok;

    ImageView iv_head;

    Button btn_choiceImg;

    UPLoadHeadImgCallBack upLoadHeadImgCallBack;

    public UpLoadHeadImgDialog(@NonNull Context context) {
        this(context, sz.szsmk.citizencard.R.style.dialog);
    }

    public UpLoadHeadImgDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(LayoutInflater.from(getContext()).inflate(sz.szsmk.citizencard.R.layout.dialog_uploadheadimg, null));

        iv_close = findViewById(R.id.iv_close);

        btn_ok = findViewById(R.id.btn_ok);

        iv_head = findViewById(R.id.iv_head);

        btn_choiceImg = findViewById(R.id.btn_choiceImg);

        EventBus.getDefault().register(this);
        initView();
    }

    private void initView() {
        btn_ok.setOnClickListener(v->{
            if(upLoadHeadImgCallBack!=null){
                upLoadHeadImgCallBack.upLoadImg(path);


                dismiss();
            }
        });

        btn_choiceImg.setOnClickListener(v->{
            if(upLoadHeadImgCallBack!=null){
                upLoadHeadImgCallBack.callback(iv_head);
            }
        });

        iv_close.setOnClickListener(v->
                UpLoadHeadImgDialog.this.dismiss());
    }

    public UPLoadHeadImgCallBack getUpLoadHeadImgCallBack() {
        return upLoadHeadImgCallBack;
    }

    public void setUpLoadHeadImgCallBack(UPLoadHeadImgCallBack upLoadHeadImgCallBack) {
        this.upLoadHeadImgCallBack = upLoadHeadImgCallBack;
    }

    public interface UPLoadHeadImgCallBack{
        void callback(ImageView imageView);
        void upLoadImg(String path);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        EventBus.getDefault().unregister(UpLoadHeadImgDialog.this);
    }

    private String path;

    @Subscribe
    public void onEventMainThread(UploadHeadImgEntry bean) {
        path  = bean.getPath();
        Glide.with(getContext())
                .load(path)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(iv_head);
        btn_ok.setVisibility(View.VISIBLE);
    }
}